<?php
class Pageley_Test_Suite {
	private $package;
	private $suiteResult;
	private $testCaseClass;
	
	public function __construct($package) {
		$this->package = $package;
		$this->testCaseClass = new ReflectionClass("Pageley_Test_Case");
	}
	
	public function getPackage() {
		return $package;
	}
	
	public function run() {
		$system = Pageley_Util_System::getInstance();
		$this->suiteResult = new Pageley_Test_SuiteResult($system->pathToResourceName($this->getName()));
		$file = new Pageley_Util_Directory($this->package);
		$this->runFile($file);
		return $this->suiteResult;
	}
	
	private function runFile($file) {
		if($file->isDirectory()) {
			$files = $file->getFiles();
			foreach($files as $file) {
				$this->runFile($file);
			}
		} else if(Pageley_Util_String::endsWith($file->getName(), ".php")) {
			$system = Pageley_Util_System::getInstance();
			$class = new ReflectionClass($system->pathToResourceName($file->getPath()));
			if($class->isSubclassOf($this->testCaseClass) && !$class->isAbstract()) {
				$testCase = $class->newInstance();
				$this->suiteResult->addTestResult($testCase->run());
			}
		}
	}
	
	public function getName() {
		return $this->package;
	}
}